package validator;

import com.baidu.unbiz.fluentvalidator.ValidationError;
import com.baidu.unbiz.fluentvalidator.Validator;
import com.baidu.unbiz.fluentvalidator.ValidatorContext;
import com.baidu.unbiz.fluentvalidator.ValidatorHandler;
import util.ValidatorUtil;

/**
 * 字符串长度校验
 * Created by admin on 2017/2/18.
 */
public class IdCardFluentValidator extends ValidatorHandler<String> implements Validator<String> {




    private String fieldName;
    private boolean power=true;

    public IdCardFluentValidator(String fieldName) {
        this.fieldName = fieldName;
    }
    public IdCardFluentValidator(String fieldName,boolean power) {
        this.fieldName = fieldName;
        this.power=power;
    }


    @Override
    public boolean validate(ValidatorContext context, String s) {

        if (!ValidatorUtil.isIdCard(s,power)) {
            context.addError(ValidationError.create(String.format("%s错误", fieldName))
                    .setErrorCode(-1)
                    .setField(fieldName)
                    .setInvalidValue(s));
            return false;
        }
        return true;
    }

}
